import datetime
import json
import decimal
import random
import string


class DealDatabaseDataType():
    """
    处理数据库数据类型
    例如datetime，Decimal类型
    """

    @staticmethod
    def __deal__(data):
        if isinstance(data, datetime.datetime):
            return data.strftime("%Y-%m-%d %H:%M:%S")

        elif isinstance(data, decimal.Decimal):
            return float(data)
        else:
            return data

    @staticmethod
    def many(data: list):
        """处理多组数据"""
        out = []
        for i in data:
            if type(i) in (tuple, dict, list):
                out.append(DealDatabaseDataType.one(i))
            else:
                raise Exception("仅仅支持类型List[dict],List[list],List[tuple]")
        return out

    @staticmethod
    def one(data):
        """处理一组数据"""
        if type(data) == dict:  # 处理数据传入dict型
            for k, v in data.items():
                data[k] = DealDatabaseDataType.__deal__(v)
        elif type(data) == list or type(data) == tuple:  # 处理数据传入list或者tuple型
            isconvert = False
            if type(data) == tuple:
                data = list(data)
                isconvert = True
            for i in range(len(data)):
                data[i] = DealDatabaseDataType.__deal__(data[i])
            if isconvert:
                data = tuple(data)
        else:
            pass

        return data


def deal_page_pagesize(page: int = 1, pagesize: int = 10):
    return pagesize, (page - 1) * pagesize


def password_random(length):
    password = ''.join(
        [random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation) for i in
         range(length)])
    return password
